package com.time.hellotime.components.discovery;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.time.hellotime.R;
import com.time.hellotime.base.BaseViewHolder;
import com.time.hellotime.base.BaseXRecAdapter;
import com.time.hellotime.servermodel.discovery.Discovery;
import com.time.hellotime.utils.ScreenUtils;
import com.time.hellotime.webserver.helper.picasso.PicassoImageLoader;

import java.util.List;

/**
 * 发现公益 - 征集中
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class CollAdapter extends BaseXRecAdapter<Discovery> {

    public CollAdapter(Context mContext, List<Discovery> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View convertView = inflater.inflate(R.layout.item_coll_list, parent, false);
        DiscoveryViewHolder publishViewHolder = new DiscoveryViewHolder(convertView);
        convertView.setTag(publishViewHolder);
        return publishViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position, boolean isItem) {
        final Discovery discovery = getItem(position);
        DiscoveryViewHolder disHolder = (DiscoveryViewHolder) holder;
        if (!TextUtils.isEmpty(discovery.getCoverPicUrl())) {
            int width = ScreenUtils.dip2px(340);
            int height = ScreenUtils.dip2px(178);
            PicassoImageLoader.loadImageViewSize(mContext,
                    discovery.getCoverPicUrl(),
                    width,
                    height,
                    R.drawable.iv_default_340_178,
                    R.drawable.iv_default_340_178,
                    toString(), disHolder.ivPic);
        }
        disHolder.tvType.setText(discovery.getType());
        disHolder.tvLocation.setText(discovery.getCity());
        disHolder.tvTitle.setText(discovery.getTitle());
        disHolder.tvSubTitle.setText(discovery.getIntroduction());
        disHolder.tvStartTime.setText(discovery.getStartDate());
        disHolder.tvExecuteCycle.setText(new StringBuilder().append(discovery.getExecutionCycle()).append("天"));
        disHolder.tvLeaveNum.setText(discovery.getLeaveNum());

        disHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener){
                    if (TextUtils.isEmpty(discovery.getId())){
                        return;
                    }else {
                        onItemClickListener.onItemClick(v, position, discovery, getViewType());
                    }
                }
            }
        });

    }

    class DiscoveryViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public ImageView ivPic;
        public TextView tvType;
        public TextView tvLocation;
        public TextView tvTitle;
        public TextView tvSubTitle;
        public TextView tvStartTime;
        public TextView tvExecuteCycle;
        public TextView tvLeaveNum;
        public View divider;

        public DiscoveryViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_coll_item_parent);
            ivPic = getView(R.id.iv_coll_pic);
            tvType = getView(R.id.tv_coll_type);
            tvLocation = getView(R.id.tv_coll_location);
            tvTitle = getView(R.id.tv_coll_title);
            tvSubTitle = getView(R.id.tv_coll_sub_title);
            tvStartTime = getView(R.id.tv_coll_start_time);
            tvExecuteCycle = getView(R.id.tv_coll_execute_cycle);
            tvLeaveNum = getView(R.id.tv_coll_leave_num);
            divider = getView(R.id.v_divider);
        }
    }
}
